(function() {
  //获取部门用户信息
  var departmentId = "2efc32ee89074e919afa2219e3cace53";
  var departmentUsers = $.contact.listUsersByDepartmentId(departmentId);
  // 获取企业微信 accessToken
  var wxworkConfig = {
    "corpid": "ww4852f35d45307a6f",
    "corpsecret": "WfGaeVl5rLqiKXvf1WCvp-5wUy_Dgoc4RJHR4t2mArc"
  }
  var tokenUri = "https://qyapi.weixin.qq.com/cgi-bin/gettoken";
  var tokenParams = new Packages.java.util.HashMap();
  var tokenHeader = new Packages.java.util.HashMap();
  tokenParams.put("corpid", wxworkConfig.corpid);
  tokenParams.put("corpsecret", wxworkConfig.corpsecret);
  var acceccRes = $.httpclient.sendGet(tokenUri, tokenParams, tokenHeader);
  var accessToken = JSON.parse(acceccRes).access_token;
  // 批量获取客户详情
  var index = 0
  var appId = $.context.getCurrentApplicationId();
  getExternalContact()
  function getExternalContact(cursor) {
    cursor = cursor == "" ? "" : cursor
    var curItem = departmentUsers.get(index)
    var uri = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/batch/get_by_user";
    var params = new Packages.java.util.HashMap();
    var headers = new Packages.java.util.HashMap();
    var json = {
      "userid": curItem.get("wxUserId"),
      "cursor": cursor,
      "limit": 100
    };
    var genderMap = ["3", "1", "2"];
    var jsonStr = $.json.objectToJsonString(json);
    headers.put("Content-Type", "application/json;charset=UTF-8");
    params.put("access_token", accessToken);
    var response = $.httpclient.sendPost(uri, params, headers, jsonStr);
    var resData = $.json.stringToJsonObject(response);
    var nextCursor = resData.get("next_cursor");
    // 循环塞数据
    var list = resData.get("external_contact_list");
    for (var i = 0; i < list.size(); i++) {
      var item = list.get(i)
      var follow = item.get("follow_info")
      var external = item.get("external_contact")
      var timestamp = follow.get("createtime") * 1000
      var doc = $.form.createEmptyDocument(appId, "企微添加客户");
      doc.addElement("客户", external.get("external_userid"));
      doc.addElement("企微", follow.get("unionid"));
      doc.addElement("员工", follow.get("userid"));
      doc.addElement("姓名", external.get("name"));
      doc.addElement("类型", external.get("type") == 1 ? "微信用户" : "企微");
      doc.addElement("性别", genderMap[external.get("gender")]);
      doc.addElement("职位", external.get("position"));
      doc.addElement("企业简称", external.get("corp_name"));
      doc.addElement("企业全称", external.get("corp_full_name"));
      doc.addElement("添加时间", timestamp);
      doc.addElement("备注", follow.get("remark"));
      $.form.saveFormDocument(doc, appId);
    }
    if (nextCursor != "") {
      getExternalContact(nextCursor)
    } else if (index < list.size()) {
      index++
      getExternalContact()
    }
  }
})()

(function() {
  // 获取部门用户信息 一共有48条
  var departmentId = "2efc32ee89074e919afa2219e3cace53";
  var departmentUsers = $.contact.listUsersByDepartmentId(departmentId);
  var appId = $.context.getCurrentApplicationId();
  var genderMap = ["3", "1", "2"];
  // 开始时间
  var startTime = $.date.getCurrentDate();
  // 目标数量
  var count = 10000;
  // 已导入数量
  var len = 0;

  while (len < count){
    len = len + saveData(departmentUsers);
  }

  // 结束任务 发送站内信
  var receiveUserIds = "b2c26e0438a654765d932c510d071eb8";
  var endTime = $.date.getCurrentDate();
  $.message.sendGeneralStationMessage("导入" + len + "条成功", "耗时" + ((endTime.getTime() - startTime.getTime()) / 1000) + "秒", receiveUserIds);

  function saveData(departmentUsers){
    var len = 0;
    for (var i = 0; i < departmentUsers.size(); i++) {
      var item = departmentUsers.get(i);
      var doc = $.form.createEmptyDocument(appId, "企微添加客户");
      doc.addElement("客户", item.getWxUserId());
      doc.addElement("员工", item.getAccount());
      doc.addElement("姓名", item.getName());
      doc.addElement("类型", "企微");
      doc.addElement("性别", genderMap[item.getGender()]);
      doc.addElement("职位", item.getPosition());
      doc.addElement("企业简称", "道一云");
      doc.addElement("企业全称", "广东道一云有限公司");
      doc.addElement("添加时间", item.getTelephone());
      doc.addElement("备注", item.getPosition());
      $.form.saveFormDocument(doc, appId);
      len++;
    }
    return len;
  }
})()

// 获取 accessToken
(function() {
  var wxworkConfig = {
    "corpid": "wx07ad350b0dfd7747",
    "corpsecret": "WfGaeVl5rLqiKXvf1WCvp-5wUy_Dgoc4RJHR4t2mArc"
  }
  var tokenUri = "https://qyapi.weixin.qq.com/cgi-bin/gettoken";
  var tokenParams = new Packages.java.util.HashMap();
  var tokenHeader = new Packages.java.util.HashMap();
  tokenParams.put("corpid", wxworkConfig.corpid);
  tokenParams.put("corpsecret", wxworkConfig.corpsecret);
  var acc = $.httpclient.sendGet(tokenUri, tokenParams, tokenHeader);
  var accessToken = JSON.parse(acc).access_token;
})

(function() {
  //获取部门用户信息
  var departmentId = "3136001a8935ba6ddf61c808711861e9";
  var departmentUsers = $.contact.listUsersByDepartmentId(departmentId);
  var departmentUsersStr = $.json.objectToJsonString(departmentUsers);
  var departmentUsersArr = $.json.stringToJsonArray(departmentUsersStr);
  // var departmentUserIds = []
  var receiveUserIds = $.context.getCurrentUserId();
  $.message.sendGeneralStationMessage("站内信标题", departmentUsersStr, receiveUserIds);

  var appId = $.context.getCurrentApplicationId();
  function append(item) {
    var doc = $.form.createEmptyDocument(appId, "企微添加客户");
    doc.addElement("姓名", item.get("name"));
    doc.addElement("企微", item.get("wxUserId"));
    $.form.saveFormDocument(doc, appId);
  }
  for (var i = 0; i < departmentUsersArr.size(); i++) {
    // departmentUserIds.push(parseDepartmentUsers.get(i).wxUserId)
    //保存表单
    var item = departmentUsersArr.get(i);
    append(item)
  }
})()